<template>
  <div>
    <Modal v-model="visible" width='800' :mask-closable="false" :closable="false" title="定时计算">
      <Row>
        <Col span="14" offset="5">
        <Form ref="model" :model="model" inline :rules="ruleInline" :label-width="100">
          <Row v-if="model.ruleId==null">
            <Col>
            <FormItem prop="rule" style="width:100%;" label="计算规则">
              <Select v-model="model.rule" placeholder="请选择" clearable style="width: 100%">
                <Option v-for="(_item,index) in rules" :key="index" :value="_item.ruleId">{{_item.ruleName}}</Option>
              </Select>
            </FormItem>
            </Col>
          </Row>
          <Row>
            <Col>
            <FormItem prop="weekNum" style="width:100%;" label="计算周期">
              <dict model="model.weekNum" :value="model.weekNum" @click="getVal" code='ZGX_WEEK' style="width:100%;"></dict>
            </FormItem>
            </Col>
          </Row>
          <Row>
            <Col>
            <FormItem prop="time" style="width:100%;" label="计算时间">
              <TimePicker type="time" placeholder="请选择" style="width:100%;" v-model="model.time"></TimePicker>
            </FormItem>
            </Col>
          </Row>
        </Form>
        </Col>
      </Row>
      <div slot="footer">
        <Button @click="cancelTask">取消</Button>
        <Button @click="saveTask" class="m-l-md" type="primary">确认</Button>
      </div>
    </Modal>
    <spin-temp v-if="loading"></spin-temp>
  </div>
</template>
<script>
import dict from "_c/dict/dict.vue";
import Api from "@/api/calculation";
const api = new Api();
export default {
  props: ["visible", "modelTemp", "rules"],
  components: {
    dict
  },
  data() {
    return {
      loading: false,
      model: this.modelTemp,
      ruleInline: {
        rule: [{ required: true, message: "请选择" }],
        weekNum: [{ required: true, message: "请选择" }],
        time: [{ required: true, message: "请选择" }]
      }
    };
  },
  watch: {
    modelTemp: function(modelTemp) {
      this.model = { ...modelTemp };
      if (typeof modelTemp.weekNum === "number") {
        this.model.weekNum = modelTemp.weekNum.toString();
      }
    }
  },
  methods: {
    getVal(expression) {
      let self = this;
      eval(expression);
      self.$forceUpdate();
    },
    saveTask() {
      let self = this;
      self.$refs["model"].validate(valid => {
        if (valid) {
          self.loading = true;
          self.model = { ...self.modelTemp, ...self.model };
          if (self.model.ruleId == null) {
            self.model.ruleId = self.model.rule;
          }
          self.model = _.omit(self.model, ["rule"]);
          api
            .scheduledEdit(self.model)
            .then(data => { 
              if (self.$router.currentRoute.name === "timing-task") {
                self.$emit("fetch");
                self.cancelTask();
              } else {
                self.$router.push(
                  "/integral-calculation-management/timing-task"
                );
              }
            })
            .catch(error => {
              self.loading = false;
            });
        }
      });
    },
    cancelTask() {
      let self = this;
      self.$emit("update:visible", false);
      self.loading = false;
      self.$refs["model"].resetFields();
    }
  }
};
</script>

